import 'dart:convert';

import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:leader_help/page/home/key_point/key_point_microcosmic/key_point_small_view.dart';
import 'package:leader_help/page/task/main/models/area/area.dart';
import 'package:leader_help/page/task/main/models/com/com.dart';
import 'package:leader_help/page/task/main/models/industry_model/industry_model.dart';
import 'package:leader_help/page/task/main/task/widgets/city_picker.dart';
import 'package:leader_help/page/task/main/task/widgets/dropdown.dart';
import 'package:leader_help/page/task/main/task/widgets/industry_picker.dart';
import 'package:leader_help/utils/util.dart';

import 'index.dart';

class ComListController extends GetxController {
  ComListController();

  final state = ComListState();

  void actionMenuTypes(int index) {
    if (state.activeIndex == index) {
      //收起
      state.activeIndex = -1;
    } else {
      //弹出
      state.activeIndex = index;
      switch (index) {
        case 0:
          showDropDownList(
              left: 15.r,
              right: 15.r,
              top: SizeFit.navBarHeight + 44.r - SizeFit.statusBarHeight,
              height: 300.h,
              needBarrierColor: true,
              selectedIndex: state.selectComTypeIndex,
              list: state.comTypes,
              onTap: (selectedIndex) {
                state.selectComTypeIndex = selectedIndex;
                update();
              }).then((value) {
            state.activeIndex = -1;
            update();
          });
          break;
        case 1:
          actionCityPickers();
          break;
        case 2:
          actionIndustryPickers();
          break;
        case 3:
          showDropDownList(
              left: 15.r,
              right: 15.r,
              top: SizeFit.navBarHeight + 44.r - SizeFit.statusBarHeight,
              height: 300.h,
              needBarrierColor: true,
              selectedIndex: state.selectScopeIndex,
              list: state.scopes,
              onTap: (selectedIndex) {
                state.selectScopeIndex = selectedIndex;
                update();
              }).then((value) {
            state.activeIndex = -1;
            update();
          });
          break;
        default:
          break;
      }
    }

    update();
  }

  void actionCityPickers() async {
    if (state.sourceAreaMod == null) {
      var val = await rootBundle.loadString('assets/jsons/area.json');
      var _json = json.decode(val);
      var _area = AreaModel.fromJson(_json);
      state.sourceAreaMod = _area;
      state.selectedMod.initData(_area);
    }
    await showCityPickers(
        left: 15.r,
        right: 15.r,
        top: SizeFit.navBarHeight + 44.r - SizeFit.statusBarHeight,
        height: 400.h,
        needBarrierColor: true,
        areaMod: state.sourceAreaMod!,
        selectedMod: state.selectedMod,
        onChoosed: (mod) {
          state.selectedMod = mod;
        });
    state.activeIndex = -1;
    update();
  }

  void actionIndustryPickers() async {
    if (state.industryList == null) {
      var val = await rootBundle.loadString('assets/jsons/industry.json');
      List _json = json.decode(val);
      var _industrys = _json.map((e) => IndustryModel.fromJson(e)).toList();
      state.industryList = _industrys;
    }
    await showIndustryPickers(
        left: 15.r,
        right: 15.r,
        top: SizeFit.navBarHeight + 44.r - SizeFit.statusBarHeight,
        height: 400.h,
        needBarrierColor: true,
        mds: state.industryList!,
        selectedMod: state.industrySelectedMod,
        onChoosed: (mod) {
          state.industrySelectedMod = mod;
        });
    state.activeIndex = -1;
    update();
  }

  /// 在 widget 内存中分配后立即调用。
  @override
  void onInit() {
    super.onInit();
    state.comTypes = [
      '不限',
      '有融资',
      '最具发展潜力(通过资本等因素综合分析得出)',
      '瞪羚企业',
      '独角兽',
      '龙头企业',
      '单项冠军',
      '专精特新',
      '专精特新小巨人'
    ];
    state.scopes = [
      '不限规模',
      '规上企业',
      '中型企业',
      '小型企业',
      '微型企业',
    ];
    state.list = [
      ComModel(
        icon: 'u650.png'.imgPathTask,
        title: '三全食品股份有限公司',
        address: '郑州-金水区',
        status: '净利润下降，缺乏创新产品',
        signs: ['最具潜力', 'IPO上市'],
      ),
      ComModel(
        icon: 'u667.png'.imgPathTask,
        title: '致欧家居科技股份有限公司',
        address: '郑州-二七区',
        status: '急需组织升级，助力上市',
        signs: ['最具潜力', 'C轮融资'],
      ),
      ComModel(
        icon: 'u682.png'.imgPathTask,
        title: '中科慧远视觉技术（洛阳）有限公司',
        address: '洛阳-涧西区',
        status: '产品创新，经营良好，建议持续关注',
        signs: ['最具潜力', 'C轮融资'],
      ),
      ComModel(
        icon: 'u692.jpeg'.imgPathTask,
        title: '河南裕华新材料股份有限公司',
        address: '焦作-博爱县',
        status: '需要进行技术升级引入新技术',
        signs: ['最具潜力', 'C轮融资'],
      ),
      ComModel(
        icon: 'u701.jpeg'.imgPathTask,
        title: '巴奴毛肚火锅',
        address: '郑州-管城',
        status: '需要资金支持以加速发展',
        signs: ['最具潜力', 'B+轮融资'],
      ),
      ComModel(
        icon: 'u714.jpeg'.imgPathTask,
        title: 'UU跑腿',
        address: '郑州-金水区',
        status: '急需组织升级，助力上市',
        signs: ['最具潜力', 'B轮融资'],
      ),
    ];
  }

  void actionCell(ComModel mod) {
    Get.to(() => const KeyPointSmallPage());
  }

  /// 在 onInit() 之后调用 1 帧。这是进入的理想场所
  @override
  void onReady() {
    super.onReady();
  }

  /// 在 [onDelete] 方法之前调用。
  @override
  void onClose() {
    super.onClose();
  }

  /// dispose 释放内存
  @override
  void dispose() {
    super.dispose();
  }
}
